var express = require('express')
var router = express.Router()
const { getAll, getOne, exec } = require('../../db/index')

// 获取所有留言
router.get('/all', async (req, res) => {
  let sql = `select * from blog_message`
  const data = await getAll(sql)
  res.send({
    code: 0,
    message: '查询成功',
    result: data,
  })
})
// 根据关键字搜索留言
router.post('/search', async (req, res) => {
  const { keyword } = req.body
  let sql = `select * from blog_message where concat(content,name) like '%${keyword}%'`
  const data = await getAll(sql)
  res.send({
    code: 0,
    message: '查询成功',
    result: data,
  })
})
// 新增留言
router.post('/addnew', async (req, res) => {
  const { name, content } = req.body
  let sql = `insert into blog_message (content,name) values('${content}','${name}')`
  await exec(sql)
  res.send({
    code: 0,
    message: '新增成功',
  })
})

module.exports = router
